/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.positionorder.service;

import java.util.Date;
import java.util.List;

import com.jeeplus.common.utils.SnowFlake;
import com.jeeplus.modules.address.entity.Address;
import com.jeeplus.modules.address.service.AddressService;
import com.jeeplus.modules.freezingassets.entity.FreezingAssets;
import com.jeeplus.modules.freezingassets.service.FreezingAssetsService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.positionassets.entity.PositionAssets;
import com.jeeplus.modules.positionassets.service.PositionAssetsService;
import com.jeeplus.modules.subgoods.entity.SubGoods;
import com.jeeplus.modules.subgoods.service.SubGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.positionorder.entity.PositionOrder;
import com.jeeplus.modules.positionorder.mapper.PositionOrderMapper;

/**
 * 资产发货订单Service
 * @author w
 * @version 2021-12-11
 */
@Service
@Transactional(readOnly = true)
public class PositionOrderService extends CrudService<PositionOrderMapper, PositionOrder> {

	@Autowired
	private PositionAssetsService positionAssetsService;
	@Autowired
	private FreezingAssetsService freezingAssetsService;
	@Autowired
	private SubGoodsService subGoodsService;
	@Autowired
	private AddressService addressService;

	public PositionOrder get(String id) {
		return super.get(id);
	}
	
	public List<PositionOrder> findList(PositionOrder positionOrder) {
		return super.findList(positionOrder);
	}
	
	public Page<PositionOrder> findPage(Page<PositionOrder> page, PositionOrder positionOrder) {
		return super.findPage(page, positionOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(PositionOrder positionOrder) {
		super.save(positionOrder);
	}
	
	@Transactional(readOnly = false)
	public void delete(PositionOrder positionOrder) {
		super.delete(positionOrder);
	}

	/**
	 * 资产申请发货
     * @param member 申请用户
     * @param flag    订单类型
     * @param positionId    资产id
     * @param applyNum  提货数量
     */
	@Transactional(readOnly = false)
	public void applyExpress(Member member, String flag, String positionId, Integer applyNum) {
		//用户地址
		Address address = new Address();
		address.setMember(member);
		List<Address> addresses = addressService.findList(address);
		if (addresses.size() <= 0){
			throw new RuntimeException("地址信息有误");
		}
		if ("0".equals(flag)){
			//持仓资产
			PositionAssets positionAssets = positionAssetsService.get(positionId);
			if (positionAssets == null){
				throw new RuntimeException("资产信息有误");
			}
			if (applyNum > positionAssets.getGoodsNum()){
				throw new RuntimeException("资产数量不足");
			}
			//持仓资产减去数量
			Integer remain = positionAssets.getGoodsNum() - applyNum;
			positionAssets.setGoodsNum(remain);
			positionAssets.setNowTotal(remain * positionAssets.getNowPrice());
			positionAssets.setApplyNum((positionAssets.getApplyNum() == null ? 0 : positionAssets.getApplyNum()) + applyNum);
			if (remain == 0){
				positionAssets.setIsApply("1");
			}
			positionAssetsService.save(positionAssets);
			//生成订单
			PositionOrder positionOrder = new PositionOrder();
			positionOrder.setMember(member);
			positionOrder.setTotalPrice(applyNum * positionAssets.getNowPrice());
			positionOrder.setPrice(positionAssets.getNowPrice());
			positionOrder.setNum(applyNum);
			positionOrder.setGoodsName(positionAssets.getGoodsName());
			positionOrder.setPositionId(positionId);
			positionOrder.setOrderType(flag);
			positionOrder.setConsignee(addresses.get(0).getName());
			positionOrder.setConsigneePhone(addresses.get(0).getMobile());
			positionOrder.setConsigneeAddress(addresses.get(0).getAddress());
			//订单号
			SnowFlake snowFlake = new SnowFlake(2, 3);
			Long orderNo = snowFlake.nextId();
			positionOrder.setOrderNo(orderNo.toString());
			positionOrder.setOrderStatus("0");
			SubGoods subGoods = subGoodsService.get(positionAssets.getSubGoods().getId());
			if (subGoods != null){
				positionOrder.setPic(subGoods.getGoodsPic());
			}
			super.save(positionOrder);
		}else {
			//锁仓资产
			FreezingAssets freezingAssets = freezingAssetsService.get(positionId);
			if (freezingAssets == null){
				throw new RuntimeException("资产信息有误");
			}
			if (applyNum > freezingAssets.getNum()){
				throw new RuntimeException("资产数量不足");
			}
			//持仓资产减去数量
			Integer remain = freezingAssets.getNum() - applyNum;
			freezingAssets.setNum(remain);
			freezingAssets.setNowTotal(remain * freezingAssets.getNowPrice());
			freezingAssets.setApplyNum((freezingAssets.getApplyNum() == null ? 0 : freezingAssets.getApplyNum()) + applyNum);
			if (remain == 0){
				freezingAssets.setIsApply("1");
			}
			freezingAssetsService.save(freezingAssets);
			//生成订单
			PositionOrder positionOrder = new PositionOrder();
			positionOrder.setMember(member);
			positionOrder.setTotalPrice(freezingAssets.getNowPrice() * applyNum);
			positionOrder.setPrice(freezingAssets.getNowPrice());
			positionOrder.setNum(applyNum);
			positionOrder.setGoodsName(freezingAssets.getGoodsName());
			positionOrder.setPositionId(positionId);
			positionOrder.setOrderType(flag);
			positionOrder.setConsignee(addresses.get(0).getName());
			positionOrder.setConsigneePhone(addresses.get(0).getMobile());
			positionOrder.setConsigneeAddress(addresses.get(0).getAddress());
			//订单号
			SnowFlake snowFlake = new SnowFlake(2, 3);
			Long orderNo = snowFlake.nextId();
			positionOrder.setOrderNo(orderNo.toString());
			positionOrder.setOrderStatus("0");
			SubGoods subGoods = subGoodsService.get(freezingAssets.getSubGoods().getId());
			if (subGoods != null){
				positionOrder.setPic(subGoods.getGoodsPic());
			}
			super.save(positionOrder);
		}
	}

	/**
	 * 保存发货
	 * @param positionOrder  订单
	 */
	@Transactional(readOnly = false)
	public void saveExpress(PositionOrder positionOrder) {
		if ("0".equals(positionOrder.getOrderStatus())){
			//待发货
			positionOrder.setOrderStatus("1");
			positionOrder.setExpressTime(new Date());
			super.save(positionOrder);
		}
	}

	/**
	 * 取消发货
	 * @param positionOrder
	 */
	@Transactional(readOnly = false)
	public void cancel(PositionOrder positionOrder) {
		if ("0".equals(positionOrder.getOrderStatus())){
			//待发货中才可取消
			positionOrder.setOrderStatus("2");//取消发货
			super.save(positionOrder);
			//对应资产还原
			if ("0".equals(positionOrder.getOrderType())){
				//持仓订单
				PositionAssets positionAssets = positionAssetsService.get(positionOrder.getPositionId());
				if (positionAssets != null){
					//返还持仓资产
					positionAssets.setGoodsNum(positionAssets.getGoodsNum() + positionOrder.getNum());
					positionAssets.setNowTotal(positionAssets.getGoodsNum() * positionAssets.getNowPrice());
					positionAssets.setApplyNum(positionAssets.getApplyNum() - positionOrder.getNum());
					positionAssets.setIsApply("0");
					positionAssetsService.save(positionAssets);
				}
			}else {
				//锁仓订单
				FreezingAssets freezingAssets = freezingAssetsService.get(positionOrder.getPositionId());
				if (freezingAssets != null){
					//返还持仓资产
					freezingAssets.setNum(freezingAssets.getNum() + positionOrder.getNum());
					freezingAssets.setNowTotal(freezingAssets.getNum() * freezingAssets.getNowPrice());
					freezingAssets.setApplyNum(freezingAssets.getApplyNum() - positionOrder.getNum());
					freezingAssets.setIsApply("0");
					freezingAssetsService.save(freezingAssets);
				}
			}
		}
	}

	/**
	 * 查询没有取消的资产申请发货订单
	 * @param positionOrder
	 * @return
	 */
	public List<PositionOrder> findNotCancelList(PositionOrder positionOrder) {
		return mapper.findNotCancelList(positionOrder);
	}


}